package com.powerbank.ordercenter.controller;

import com.powerbank.common.dto.CreateOrderRequest;
import com.powerbank.common.dto.OrderDTO;
import com.powerbank.common.dto.ReturnOrderRequest;
import com.powerbank.common.result.PageResult;
import com.powerbank.common.result.Result;
import com.powerbank.ordercenter.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制器
 */
@Tag(name = "订单管理", description = "订单相关接口")
@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @Operation(summary = "创建订单", description = "用户创建租借订单")
    @PostMapping
    public Result<OrderDTO> createOrder(@Validated @RequestBody CreateOrderRequest request) {
        return orderService.createOrder(request);
    }

    @Operation(summary = "支付订单", description = "用户支付订单")
    @PostMapping("/{orderNo}/pay")
    public Result<Void> payOrder(
            @Parameter(description = "订单号") @PathVariable String orderNo,
            @Parameter(description = "支付ID") @RequestParam String paymentId) {
        return orderService.payOrder(orderNo, paymentId);
    }

    @Operation(summary = "开始使用", description = "用户开始使用设备")
    @PostMapping("/{orderNo}/start")
    public Result<Void> startOrder(@Parameter(description = "订单号") @PathVariable String orderNo) {
        return orderService.startOrder(orderNo);
    }

    @Operation(summary = "归还设备", description = "用户归还设备")
    @PostMapping("/return")
    public Result<OrderDTO> returnOrder(@Validated @RequestBody ReturnOrderRequest request) {
        return orderService.returnOrder(request);
    }

    @Operation(summary = "取消订单", description = "用户取消订单")
    @PostMapping("/{orderNo}/cancel")
    public Result<Void> cancelOrder(
            @Parameter(description = "订单号") @PathVariable String orderNo,
            @Parameter(description = "取消原因") @RequestParam String reason) {
        return orderService.cancelOrder(orderNo, reason);
    }

    @Operation(summary = "查询订单详情", description = "根据订单号查询订单详情")
    @GetMapping("/{orderNo}")
    public Result<OrderDTO> getOrderByNo(@Parameter(description = "订单号") @PathVariable String orderNo) {
        return orderService.getOrderByNo(orderNo);
    }

    @Operation(summary = "查询用户订单列表", description = "分页查询用户订单列表")
    @GetMapping("/user/{userId}")
    public Result<PageResult<OrderDTO>> getUserOrders(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer size) {
        return orderService.getUserOrders(userId, page, size);
    }

    @Operation(summary = "查询用户活跃订单", description = "查询用户当前活跃的订单")
    @GetMapping("/user/{userId}/active")
    public Result<List<OrderDTO>> getUserActiveOrders(
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        return orderService.getUserActiveOrders(userId);
    }

    @Operation(summary = "查询设备当前订单", description = "根据设备ID查询当前订单")
    @GetMapping("/device/{deviceId}/current")
    public Result<OrderDTO> getCurrentOrderByDevice(
            @Parameter(description = "设备ID") @PathVariable Long deviceId) {
        return orderService.getCurrentOrderByDevice(deviceId);
    }

    @Operation(summary = "计算订单费用", description = "实时计算订单费用")
    @GetMapping("/{orderNo}/fee")
    public Result<OrderDTO> calculateOrderFee(@Parameter(description = "订单号") @PathVariable String orderNo) {
        return orderService.calculateOrderFee(orderNo);
    }
}